package com.camel.base.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.camel.base.feign.CtiManagerService;
import com.camel.base.model.ZsOption;
import com.camel.base.model.ZsQueue;
import com.camel.base.model.ZsSeat;
import com.camel.base.service.ZsOptionService;
import com.camel.base.service.ZsQueueService;
import com.camel.base.service.ZsSeatService;
import com.camel.core.controller.BaseCommonController;
import com.camel.core.entity.Result;
import com.camel.core.exceptions.CustomException;
import com.camel.core.service.BaseCommonService;
import com.camel.core.utils.ResultUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author camel
 */
@RestController
@RequestMapping("zsQueue")
public class ZsQueueController extends BaseCommonController<ZsQueue> {

    @Autowired
    private ZsQueueService<ZsQueue> service;

    @Autowired
    private ZsSeatService<ZsSeat> seatService;

    @Autowired
    private CtiManagerService ctiManagerService;

    @Override
    public boolean deleteAble(Serializable id) {
        ZsQueue byId = service.getById(id);
        if (ObjectUtils.isEmpty(byId)) {
            throw new CustomException("删除失败: " + getMouduleName() + "不存在");
        }
        return true;
    }

    @PostMapping("sync")
    public Result sync(@RequestBody JSONObject params) {
        JSONArray names = params.getJSONArray("queueNames");
        service.remove(new QueryWrapper<>());
        for (Object name : names) {
            ZsQueue zsQueue = new ZsQueue();
            zsQueue.setQueueNum((String) name);
            zsQueue.setQueueName((String) name);
            super.save(zsQueue);
        }
        return ResultUtil.success("操作成功");
    }

    @Autowired
    private ObjectMapper objectMapper;

    @PostMapping("syncQueue")
    public Result syncQueue(@RequestBody JSONObject params) {
        Result queueSeats = ctiManagerService.getQueueSeats(new JSONObject());
        List<LinkedHashMap> queues = (ArrayList<LinkedHashMap>) queueSeats.getData();
        if(!CollectionUtils.isEmpty(queues)) {
            for (LinkedHashMap map : queues) {
                ZsQueue item = objectMapper.convertValue(map, ZsQueue.class);
                ZsQueue queue = service.getOne(new QueryWrapper<ZsQueue>().eq("queue_num", item.getQueueNum()), false);
                if(ObjectUtils.isEmpty(queue)) {
                    service.save(item);
                    queue = item;
                } else {
                    service.updateById(queue);
                }
                if(!CollectionUtils.isEmpty(item.getAgents())) {
                    List<ZsSeat> agents = item.getAgents();
                    for (ZsSeat seat: agents) {
                        ZsSeat one = seatService.getOne(new QueryWrapper<ZsSeat>().eq("agentnum", seat.getAgentnum()).eq("queue_id", queue.getId()));
                        if(!ObjectUtils.isEmpty(one)) {
                            one.setQueueId(queue.getId());
                            one.setUpdateAt(new Date());
                            one.setAgentname(seat.getAgentname());
                            if(one.getId().equals(182)) {
                                System.out.println(182);
                            }
                            seatService.updateById(one);
                        } else {
                            seat.setQueueId(queue.getId());
                            seat.setUpdateAt(new Date());
                            seat.setCreateAt(new Date());
                            seat.setPassword("svdata123");
                            seatService.save(seat);
                        }
                    }
                }
            }
        }

        return queueSeats;
    }

    @Override
    public BaseCommonService<ZsQueue> getiService() {
        return service;
    }

    @Override
    public String getMouduleName() {
        return "队列";
    }
}
